<?php
/**
 * Oryx Articles Module 
 * @version 0.1
 * @author Richard Adams
 */
class Articles extends Controller {

	/**
	 * Class Constructor
	 * @return 
	 */
	public function __construct()
	{
		parent::Controller();
		$this->load->model('article');	
		$this->auth->check();
		if(!$this->auth->check('level',4)) 
		{
			show_auth_error('admin/article - dont have access');
		}
	}
	
	/**
	 * Articles Default Page
	 * @return 
	 */
	public function index()
	{
		// loads the view function 
		//$this->view();
		try {
            
			try {
            
				throw new Exception('Division by b.', 23);
	
	        } catch (Exception $e) {
	            
				throw $e;
	        }

        } catch (Exception $e) {
            
			echo $e->getMessage();
			echo $e->getCode();
			echo $e->getFile();
			echo $e->getLine(); 
			echo $e->getTrace();
			echo $e->getTraceAsString();
        }
	}
	
	/**
	 * View all articles or individual artical    
	 * @return 
	 * @param object $id[optional] Individual id for article
	 */
	public function view($id = null)
	{
		if ($id != null) 
		{
			$data['content'] = $this->article->retrieve_by_pkey($id); 
			$this->oryx->render('articles/view',$data,'admin');
		}
		else
		{
			$data['content'] = $this->article->findAll();
			$this->oryx->render('articles/view_all',$data,'admin');
		}
		
	}
	
	/**
	 * Add new Article
	 * @return 
	 */
	public function add()
	{
		$this->form_validation->set_rules('title', 'Article Title', 'required');
		$this->form_validation->set_rules('content', 'Content', 'required');
		$this->form_validation->set_rules('status', 'Status', 'required');

		if ($this->form_validation->run() == FALSE)
		{
			$data['error'] = '';
			$this->oryx->render('articles/add',$data,'admin');
		}
		else
		{
			$data = $this->get_form_values();
			
			$data['date_created']= date('Y-m-d');

	        $this->article->add($data);
			$data = $this->clear_form();
			$data['error'] = 'add true';
	        $this->oryx->render('articles/add',$data,'admin');
		}
	}
	
	/**
	 * Edit an Article 
	 * @return 
	 * @param object $id[optional] Individual id for article
	 */
	public function edit($id = null)
	{
		if($id == null)
		{
			show_error('admin/article/edit - no id given');
		}
		
		$this->form_validation->set_rules('title', 'Article Title', 'required');
		$this->form_validation->set_rules('content', 'Content', 'required');
		$this->form_validation->set_rules('status', 'Status', 'required');

		if ($this->form_validation->run() == FALSE)
		{
			$data['error'] = '';
			$data['content'] = $this->article->retrieve_by_pkey($id); 
			$this->oryx->render('articles/edit',$data,'admin');
		}
		else
		{
			$data = $this->get_form_values();
			
	        $this->article->modify($id, $data);
	
			$data['error'] = 'no';
			$data['content'] = $this->article->retrieve_by_pkey($id);
	        $this->oryx->render('articles/edit',$data,'admin');
		}
		
	}
	
	/**
	 * Delete Article
	 * @return 
	 * @param object $id[optional] Individual id for article
	 */
	public function delete($id = null)
	{
		if($id == null)
		{
			show_error('admin/article/delete - no id given');
		}
		
		if ($this->article->delete_by_pkey($id)) 
		{
			show_error('admin/article/delete - Yes! delete!');
		}
		else
		{
			show_error('admin/article/delete - could not delete');
		}

	}
	
	/**
	 * gets the form values
	 * @return array form data
	 */
	private function get_form_values() 
	{
		// XXS Filtering enforced for user input
		//$data['article_id']		= $this->input->post('article_id', TRUE);
		$data['title']		= $this->input->post('title', TRUE);
		$data['url_title']		= url_title($data['title']);
		$data['excerpt']		= $this->input->post('excerpt', TRUE);
		$data['content']		= $this->input->post('content', TRUE);
		$data['status']		= $this->input->post('status', TRUE);
		$data['date_updated']		= date('Y-m-d');
		$data['section_id']		 = '1';
		$data['image_id']		= '';
		$data['user_id']		=  $this->session->userdata('user_id');

	   return $data;

	 }
	
	/**
	 * clears form array
	 * @return array empty
	 */
	private function clear_form() 
	{
		$data['article_id']		= '';
		$data['title']		= '';
		$data['url_title']		= '';
		$data['excerpt']		= '';
		$data['content']		= '';
		$data['status']		= '';
		$data['date_created']		= '';
		$data['date_updated']		= '';
		$data['section_id']		 = '';
		$data['image_id']		= '';
		$data['user_id']		= '';

	    return $data;

	}
}